package h0710;

import java.util.*;
/**
  * @description 堆栈中的剩余数字
  * @author 不知名帅哥
  * @date 2024/7/11 14:20
  * @version 1.0
*/

public class RemainNumbersInStack {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String s = sc.nextLine();
        int[] array = Arrays.stream(s.split(" ")).mapToInt(Integer::parseInt).toArray();
        getResult(array);
    }

    private static void getResult(int[] array) {
        LinkedList<Integer> stack=new LinkedList<>();
        stack.add(array[0]);

        for (int i = 1; i < array.length; i++) {
            push(array[i],stack);
        }

        StringJoiner joiner = new StringJoiner(" ");
        while (stack.size()>0) {
            joiner.add(stack.removeLast()+"");
        }
        System.out.println(joiner.toString());
    }

    private static void push(int num, LinkedList<Integer> stack) {
        int sum=num;
        for (int i = stack.size()-1; i >=0 ; i--) {
            sum-=stack.get(i);
            if (sum==0){
                //从i到栈的末尾去除掉
                stack.subList(i,stack.size()).clear();
                //将num*2push进去，继续判断加入后是否仍满足要求，所以需要递归
                push(num*2,stack);
                return;   //这里必须加return，要不然会将判断出来的n1在最后步骤加入到栈中。
            }else if (sum<0){
                break;
            }
        }
        stack.add(num);
    }

}
